package com.itlong.cloud.tapd.service;


import com.itlong.cloud.tapd.common.Result;
import com.itlong.cloud.tapd.util.GetCount;
import com.itlong.cloud.tapd.util.GetMonthlyTaskCount;
import com.itlong.cloud.tapd.util.GetTime;
import com.itlong.cloud.tapd.util.HttpSendUtil;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

/**
 * <des>
 *    统计一周完成的工单数
 * <des>
 *
 * @create Date 2021-09-07
 **/
@Service
public class CompletedWorkOrdersNumberService {



     List<String> userList= new ArrayList<>();


     List<String> workspaceidList = new ArrayList<>();

    public    List<String> getUserList(){
        if(userList.isEmpty()) {
            synchronized (CompletedWorkOrdersNumberService.class) {
                if(userList.isEmpty()) {
                    userList = getUserWorkspace();
                }
            }
        }
        return userList;
    }

    public  List<String> getWorkspaceidList(){
        if(workspaceidList.isEmpty()) {
            synchronized (CompletedWorkOrdersNumberService.class) {
                if(workspaceidList.isEmpty()) {
                    workspaceidList = getWorkspacesProjects();
                }
            }
        }
        return workspaceidList;
    }

    /**
     * <desc>
     *      获取公司成员名字列表
     * </desc>
     * @param
     * @return: java.util.List<com.itlong.cloud.tapd.common.POJO.UserWorkspacePO>
     * @author LiuYu
     * @date: 2021-09-07
     **/
    public  List<String> getUserWorkspace(){
        String url = "https://api.tapd.cn/workspaces/users?workspace_id=52784121";
        Result result =HttpSendUtil.send(url);
        Iterator iterator =  ((List<Object>) result.getData()).iterator();
        while (iterator.hasNext()){
            HashMap<String,Object> map = (HashMap<String, Object>) iterator.next();
            HashMap<String,Object> map1 = (HashMap<String, Object>) map.get("UserWorkspace");
            String user = (String) map1.get("user");
            if(("罗泽强王斌：梁杰欣叶文博苏睿：李梓豪艾诗高马驹：杜可怡王李阳柯浩妮刘伟城林嘉乐朱亚军陈树新张艺：何超明万一凡杨修：宋居光刘璎文汪炜杰郭建雄钟佳：杨启涌丁涛：张小青杨用武古通英周春：李昌旭方武：张锦添张志辉吴广彬" +
                    "甘文思柯伟鑫刘彪：刘雨：王攀：张艳：廖超：李佳芮张航博杨舒晨唐雄杰黄丽玉张永星范建庆").contains(user)) {
                userList.add(user);
            }
        }
        return userList;
    }

    /**
     * <desc>
     *      获取公司所有项目id
     * </desc>
     * @param
     * @return: java.util.List<java.lang.String>
     * @author LiuYu
     * @date: 2021-09-07
     **/
    public  List<String> getWorkspacesProjects(){
        String url = "https://api.tapd.cn/workspaces/projects?company_id=52784121";
        Result result =HttpSendUtil.send(url);
        Iterator iterator =  ((List<Object>) result.getData()).iterator();
        while (iterator.hasNext()){
            HashMap<String,Object> map = (HashMap<String, Object>) iterator.next();
            HashMap<String,Object> map1 = (HashMap<String, Object>) map.get("Workspace");
            workspaceidList.add((String) map1.get("id"));
        }
        return workspaceidList;
    }



    /**
     * 根据类型获取任务
     * @return
     */
    public List<HashMap<String,Object>>  getTasks(List<String> workspaceidLiist,Integer type){
        List<HashMap<String,Object>> list = new ArrayList<>();
        String url = new String();
        for (String workspace_id:workspaceidLiist){
            if(type == 1) {
                url = "https://api.tapd.cn/tasks?workspace_id="+workspace_id+"&completed=>"+GetTime.getSevenDaysAgo()+"&limit=200";
            }else if (type ==2){
                url = "https://api.tapd.cn/tasks?workspace_id="+workspace_id+"&status=open&due=>"+GetTime.getNow()+"&limit=200";
            }else if(type ==3){
                url = "https://api.tapd.cn/tasks?workspace_id="+workspace_id+"&status=<>done&due=<"+GetTime.getNow()+"&limit=200";
            }else if(type == 4){
                url= "https://api.tapd.cn/tasks?workspace_id="+workspace_id+"&completed="+GetTime.getFirstDayOfLastMonth()+"~"+GetTime.getLastDayOfLastMonth()+"&limit=200";
            }
            int page=2;
            Result result =HttpSendUtil.getSend(url);
            List<Object> data = ((List<Object>) result.getData());
            Iterator iterator = data.iterator();
            while (iterator.hasNext()) {
                HashMap<String, Object> map = (HashMap<String, Object>) iterator.next();
                HashMap<String, Object> map1 = (HashMap<String, Object>) map.get("Task");
                HashMap<String, Object> map2 = new HashMap<>();
                map2.put("id", map1.get("id"));
                map2.put("owner", map1.get("owner"));
                map2.put("completed", map1.get("completed"));
                map2.put("due", map1.get("due"));
                list.add(map2);
            }
            }
        return list;
    }

    /**
     * 根据类型获取需求
     *
     * @return
     */
    public List<HashMap<String, Object>> getStories(List<String> workspaceidLiist, Integer type) {
        List<HashMap<String, Object>> list = new ArrayList<>();
        String url = new String();
        for (String workspace_id : workspaceidLiist) {
            if (type == 1) {
                url = "https://api.tapd.cn/stories?workspace_id="+workspace_id+"&completed=>" + GetTime.getSevenDaysAgo()+"&limit=200";
            } else if (type == 2) {
                url = "https://api.tapd.cn/stories?workspace_id="+workspace_id+"&status=planning&due=>" + GetTime.getNow()+"&limit=200";
            } else if (type == 3) {
                url = "https://api.tapd.cn/stories?workspace_id="+workspace_id+"&status=<>resolved&due=<" + GetTime.getNow()+"&limit=200";
            }else if(type == 4){
                url = "https://api.tapd.cn/stories?workspace_id="+workspace_id+"&completed="+GetTime.getFirstDayOfLastMonth()+"~"+GetTime.getLastDayOfLastMonth()+"&limit=200";
            }
            Result result = HttpSendUtil.getSend(url);
            Iterator iterator = ((List<Object>) result.getData()).iterator();
            while (iterator.hasNext()) {
                HashMap<String, Object> map = (HashMap<String, Object>) iterator.next();
                HashMap<String, Object> map1 = (HashMap<String, Object>) map.get("Story");
                HashMap<String, Object> map2 = new HashMap<>();
                map2.put("id", map1.get("id"));
                map2.put("owner", map1.get("owner"));
                map2.put("completed",map1.get("completed"));
                map2.put("due",map1.get("due"));
                list.add(map2);
            }
        }
        return list;
    }

    /**
     * <desc>
     * 根据类型获取缺陷
     * </desc>
     *
     * @param workspaceidLiist
     * @return: java.util.List<java.util.HashMap < java.lang.String , java.lang.Object>>
     * @author LiuYu
     * @date: 2021-09-08
     **/
    public List<HashMap<String, Object>> getBugs(List<String> workspaceidLiist, Integer type) {
        List<HashMap<String, Object>> list = new ArrayList<>();
        String url = new String();
        for (String workspace_id : workspaceidLiist) {
            if (type == 1) {
                url = "https://api.tapd.cn/bugs?workspace_id="+workspace_id+"&resolved=>" + GetTime.getSevenDaysAgo()+"&limit=200";
            } else if (type == 2) {
                url = "https://api.tapd.cn/bugs?workspace_id="+workspace_id+"&status=new&due=>" + GetTime.getNow()+"&limit=200";
            } else if (type == 3) {
                url = "https://api.tapd.cn/bugs?workspace_id="+workspace_id+"&status=<>resolved&due=<" + GetTime.getNow()+"&limit=200";
            }else if(type==4){
                url = "https://api.tapd.cn/bugs?workspace_id="+workspace_id+"&resolved="+GetTime.getFirstDayOfLastMonth()+"~"+GetTime.getLastDayOfLastMonth()+"&limit=200";
            }
            Result result = HttpSendUtil.getSend(url);
            Iterator iterator = ((List<Object>) result.getData()).iterator();
            while (iterator.hasNext()) {
                HashMap<String, Object> map = (HashMap<String, Object>) iterator.next();
                HashMap<String, Object> map1 = (HashMap<String, Object>) map.get("Bug");
                HashMap<String, Object> map2 = new HashMap<>();
                map2.put("id", map1.get("id"));
                map2.put("owner", map1.get("current_owner"));
                map2.put("resolved",map1.get("resolved"));
                map2.put("due",map1.get("due"));
                list.add(map2);
            }
        }
        return list;
    }


    /**
     * <desc>
     *     告警,类型1
     * </desc>
     * @param
     * @return: java.util.List<com.itlong.cloud.tapd.common.POJO.WorkOrderNoticeVO>
     * @author LiuYu
     * @date: 2021-09-08
     **/
    public HashMap<String,Integer> getTasksDoneCount(List<String> userList,List<String> workspaceidList) throws ParseException {
        List<HashMap<String,Object>> taskList = getTasks(workspaceidList,1);
        List<HashMap<String,Object>> storyList = getStories(workspaceidList,1);
        List<HashMap<String,Object>> bugList = getBugs(workspaceidList,1);
        //告警人员列表
        HashMap<String,Integer> map = new HashMap<>();
        for (String user:userList){
            Integer count=0;
            count= GetCount.GetCountNumber(user,taskList,storyList,bugList);
            if(count<2){
                map.put(user,count);
            }
        }
        return map;
    }

    /**
     * <desc>
     *      统计未来7天内的单数，类型2
     * </desc>
     * @param
     * @return: java.util.HashMap<java.lang.String,java.lang.Integer>
     * @author LiuYu
     * @date: 2021-09-08
     **/
    public HashMap<String,Integer> getTasksToDoCount(List<String> userList,List<String> workspaceidList) throws ParseException {
        List<HashMap<String,Object>> taskList = getTasks(workspaceidList,2);
        List<HashMap<String,Object>> storyList = getStories(workspaceidList,2);
        List<HashMap<String,Object>> bugList = getBugs(workspaceidList,2);
        //告警人员列表
        HashMap<String,Integer> map = new HashMap<>();
        for (String user:userList){
            Integer count=0;
            count= GetCount.GetCountNumber(user,taskList,storyList,bugList);
            if(count<2){
                map.put(user,count);
            }
        }
        return map;
    }

    /**
     * <desc>
     *  统计未完成的任务单，类型3
     * </desc>
     * @param
     * @return: java.util.HashMap<java.lang.String,java.lang.Integer>
     * @author LiuYu
     * @date: 2021-09-08
     **/
    public HashMap<String,Integer> getTasksNotFinishedCount(List<String> userList,List<String> workspaceidList) throws ParseException{
        List<HashMap<String,Object>> taskList = getTasks(workspaceidList,3);
        List<HashMap<String,Object>> storyList = getStories(workspaceidList,3);
        List<HashMap<String,Object>> bugList = getBugs(workspaceidList,3);
        //告警人员列表
        HashMap<String,Integer> map = new HashMap<>();
        for (String user:userList){
            Integer count=0;
            count= GetCount.GetCountNumber(user,taskList,storyList,bugList);
            if(count>=1){
                map.put(user,count);
            }
        }
        return map;
    }

    /**
     * <desc>
     *      1.本月总共完成单数，超期完成单数 类型4
     *      2.缺单统计(少于22单)
     * </desc>
     * @param userList
     * @param workspaceidList
     * @return: java.util.HashMap<java.lang.String,java.lang.String>
     * @author LiuYu
     * @date: 2021-09-09
     **/
    public HashMap<String,HashMap<String,String>> getMonthlyTasksDoneCount(List<String> userList,List<String> workspaceidList) throws ParseException{
        HashMap<String,String> taskDonemap = new HashMap<>();
        HashMap<String,String> missingOrderMap = new HashMap<>();
        HashMap<String,HashMap<String,String>> map = new HashMap<>();
        List<HashMap<String,Object>> taskList = getTasks(workspaceidList,4);
        List<HashMap<String,Object>> storyList = getStories(workspaceidList,4);
        List<HashMap<String,Object>> bugList = getBugs(workspaceidList,4);
        for (String user:userList) {
            int count = 0;
            int count1 = 0;
            HashMap<String,Integer> countmap = GetMonthlyTaskCount.getCount(user,taskList,storyList,bugList);
            count = countmap.get("count");
            count1= countmap.get("count1");
            taskDonemap.put(user, "完成" + (count+count1) + "," + "超期" + count1);
            int miss=(22-count-count1)>0?(22-count-count1):0;
            DecimalFormat decimalFormat = new DecimalFormat(".00");//构造方法的字符格式这里如果小数不足2位,会以0补足.
            String missrate = miss+"%";
            if(miss>0) {
                missrate = decimalFormat.format(((float) miss / 22) * 100) + "%";
            }
            missingOrderMap.put(user,miss+"   "+"缺单率"+","+missrate);
        }
        map.put("taskDonemap",taskDonemap);
        map.put("missingOrderMap",missingOrderMap);
        return map;
    }
}
